package com.haikesi.api.timertask.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
//import com.haikesi.api.timertask.domain.vo.TimerTaskAddVO;
import com.haikesi.api.timertask.domain.po.TimerTaskPO;
import com.haikesi.api.timertask.domain.vo.*;
import com.haikesi.api.timertask.service.ITimerTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(description = "定时任务相关接口", tags = ApiIndex.TimerTaskController)
@RequestMapping(value = "/api/timerTask")
@RestController
public class TimerTaskController {

    @Autowired
    private ITimerTaskService service;

    @ApiOperation(value = "查询定时任务列表")
    @GetMapping(value = "/list")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<TimerTaskVO>>> list(@ApiParam(value = "查询参数") @ModelAttribute TimerTaskSearchVO timerTaskSeacherVO) {
        List<TimerTaskVO> list = service.getList(timerTaskSeacherVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

    @ApiOperation(value = "根据定时任务id查询模式列表")
    @GetMapping(value = "/modeList")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<TimerTaskVO>>> modeList(@ApiParam(value = "查询参数") @ModelAttribute TimerTaskModeSearchVO timerTaskSeacherVO) {
        List<TimerTaskVO> list = service.getModeList(timerTaskSeacherVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

    @ApiOperation(value = "添加定时任务")
    @PostMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R addTimerTask(@ApiParam(value = "添加参数") @Valid @RequestBody TimerTaskAddVO vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        TimerTaskPO timerTaskPO = service.addTimerTask(vo);
        return new R(timerTaskPO);
    }

    @ApiOperation(value = "修改定时任务")
    @PutMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R updateTimerTask(@ApiParam(value = "修改参数") @Valid @RequestBody TimerTaskUpdateVO vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.updateTimerTask(vo);
        return new R();
    }

    @ApiOperation(value = "删除定时任务")
    @DeleteMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R delete(@ApiParam(value = "定时任务ID") @RequestParam String id) {
        service.delete(id);
        return new R();
    }


}
